package com.mune.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.mune.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

  //在支付宝创建的应用的id
  private String app_id = "2021000116681758";

  // 商户私钥，您的PKCS8格式RSA2私钥
  private String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCMWRfP5sNzbDflzvD1S1XKvgtUBVR+cCwVqBb+M3banAX/zVXmTGpk1qhCreNP7xGz15h4VKdrpmLGoPwehhRcUU3GqZWDf+sWnsNpMrHRhrY/CTfY0mEapCxfM5FmkPq9f4eBxDFSVy1VDUiZXldEKcqk9+XAPVuHtPjvzwZdcarG8QFpUZZZGTLDOvrjnXW3Dmyix2yXku8hu/zT84Pca9zd2O8TNX+OSdoZbIEgG1DIk4Aw+np3aLunrGzFyfS75GWg+AWnA1uknZ1SDn7+JrgPwaeoLAD6quj62y3mII2dQ1eXTgQJ3PK4ShTQQjnZ199NTtjzpixpP/aMX3adAgMBAAECggEAC50HsmVnAPMQgQyptbKBK+Cj8VTrk/bWPwiEto4tpJNKj8s+82qZVzTIAZR//cpLjbUmYbanj+nZvJPvqNrxbl4v8GNyAsqf+mQyyU6vliXuiLLLkz59WIYDQfFzc6hFh065OMyyRB0AnEwYGNDMqdBHe3Uj5s9s9oLMIpPf8/VnjI//zmSaycgxE7KS6cSlyScorpcb1W+jIKFdUvPIAlTp87tmPUXrDYEzEFAkeHP2uSInTDGWl5nXAEsL02L704QbjZuh5UVyLooM4K3aJ9tynqPpdh/STSQOoaWGyP318i9qZUw4IQu+xtIH0dUZLS3PfdWqK7HnpS3STVakBQKBgQC98z5ERDuD3PJXU48cCVWg/uP/0yia7cqQlmPPIB0dz9TLu6Ptw/ZJ/Mq//nyfCpMCZ8abInlxM1nu5y06uUr3qhXyCPcvVZQq2DRpYH2oSnKFQsB1tyYGoWgvizpY0thvuYB+Tx/lB1+0VenVYQ2PXboaF6+UlrxG1bAvFZv84wKBgQC9JmzPEJRLHUSalHYrBw2NWlJ2vHbo3O6ghrHrgRXwsFpAc3rTHCmGsjLWPtnxQaZeSDYxFvWZSJG4xwJUQMMxET47ntbk6M8Xr3Zmp+/HR3p9YfZ5dRflcCOIHpFH83oETAXatjBqAhH/zyYDHHT16FBYCbZ+CDjPSoLqk/CWfwKBgF7hgdjXvGLBmsvEEVgP0ZX5vCmtcDrzTJusg26Su/iIN+MMd3XszLlobF464EURIx2weKRFNCQ3YlTTZORQagIME6oykEY0mS5NkOCSMjI9/bZTAY/LTFNDjKczBJIZv8MPBgARxfzBEi0iPQs4z20thChEbI/sO1+mnbrnMGPBAoGAI2bPsqmdt7lQn8Xtn/v/81bWXFbFwqunFfBf2H4ehx/apHl4oY6Bzy4HCcZVsCvjYX4Odn2flgLZE0ZcDzzXy5U4mpwOCGXybLJwbKI95IRiTplJMk5SHKUyYUWnV1ogmjFIXzj+MrkUeVZh/O8UjPngH7ohp9EOIO4HNrRdsS0CgYAnSfUz6uiGBisLKhK56xxEstlfg7wkmPfpJLfakFSwbWVqyA8tkGbspJxau+Yo/rjtAZCI2a3Iz15+McndJXerP11cEOr8vjLr9+BCYTcD3s0NpDngwRffJ2QbCxjS4ik4sWLncYXCD9SFJjSB98YIxBb0CqRmku8P89mufBhvWA==";
  // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAroRjHgGmlftMSTTHo+7O2jRfr5t00r5t6fl8zex5W7p2MfvGIUdLoe/oOMjJrhoOh2C+cGrg7PgDWgt3VAvCzZAhxx5sFe8UOEKqmhEpthTfAxOgNjhtB3zw6f/Fd+5JNj/kFa/XFEeEN2cJHisR5qxAQrSyH8oGf27zaKUVeQP9oRtjMybHV/V7OXkAJ2JqIbb4gi0lKE/8P30sbGVyeI9bou1oOEEaosEo9L9jdAMrUxT8SSMppuDRVmfnSFq2cpV7Q0102LPTXLSA2iHtjtP1liROeVEsTn9iB12+l62Jw/2o41E+SpF10792mghsT0V1vdBQQdDMrMm81Zk89QIDAQAB";
  // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
  // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
  private String notify_url = "http://sffj8fhhln.52http.tech/payed/notify";

  // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
  //同步通知，支付成功，一般跳转到成功页
  private String return_url = "http://member.gulimall.com/memberOrder.html";

  // 签名方式
  private String sign_type = "RSA2";

  // 字符编码格式
  private String charset = "utf-8";

  // 支付宝网关； https://openapi.alipaydev.com/gateway.do
  private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
  // 订单超时时间
  private String timeout = "1m";

  public String pay(PayVo vo) throws AlipayApiException {

    //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
    //1、根据支付宝的配置生成一个支付客户端
    AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
            app_id, merchant_private_key, "json",
            charset, alipay_public_key, sign_type);

    //2、创建一个支付请求 //设置请求参数
    AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
    alipayRequest.setReturnUrl(return_url);
    alipayRequest.setNotifyUrl(notify_url);

    //商户订单号，商户网站订单系统中唯一订单号，必填
    String out_trade_no = vo.getOut_trade_no();
    //付款金额，必填
    String total_amount = vo.getTotal_amount();
    //订单名称，必填
    String subject = vo.getSubject();
    //商品描述，可空
    String body = vo.getBody();

    alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
            + "\"total_amount\":\"" + total_amount + "\","
            + "\"subject\":\"" + subject + "\","
            + "\"body\":\"" + body + "\","
            + "\"timeout_express\":\"" + timeout + "\","
            + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

    String result = alipayClient.pageExecute(alipayRequest).getBody();

    //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
    System.out.println("支付宝的响应：" + result);

    return result;

  }
}
